package ecstore.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import ecstore.client.DataStorageService;
import ecstore.client.dataOperation.ItemInfo;
import ecstore.server.dataOperation.Item;
import ecstore.server.dataOperation.PMF;


@SuppressWarnings("serial")
public class DataStorageServiceImpl extends RemoteServiceServlet
	implements DataStorageService {


	public String DataStorageService(String name)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}
	

	public Boolean addItemService(String category, String brand, String name, float price, String size, String description,
			String thumbnail, String image, int itemStock, int itemRecmdStock)
		throws IllegalArgumentException{
		Item item = new Item(category, brand, name, price, size, description, thumbnail, image, itemStock, itemRecmdStock);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		
        try {
            pm.makePersistent(item);
        } finally {
            pm.close();
        }
        
		return true;
	}
	

	public Boolean addItemService(String category, String brand, String name, float price, String size, String description,
			String thumbnail, String image, int window, int row, int col, int itemStock, int itemRecmdStock)
		throws IllegalArgumentException{
		Item item = new Item(category, brand, name, price, size, description, thumbnail, image, window, row, col, itemStock, itemRecmdStock);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
        try {
            pm.makePersistent(item);
        } finally {
            pm.close();
        }
        
		return true;
	}


	public List<ItemInfo> getTobacco() throws IllegalArgumentException {
		List<ItemInfo> tobacco = new ArrayList<ItemInfo>();

		List<Item> queryResult;
		Iterator itr;
		Item item;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Query qry = pm.newQuery(Item.class);
		qry.setFilter("category == Tobacco");
		
		queryResult = (List<Item>) qry.execute();
		itr = queryResult.iterator();
		
		while(itr.hasNext()){
			ItemInfo itemInfo;

			item = (Item) itr.next();
			itemInfo = new ItemInfo(item.getCategory(), item.getBrand(), item.getName(),
					item.getPrice(), item.getSize(), item.getDescription(),
					item.getThumbnail(), item.getImage(), item.getWindowNum(), item.getRowNum(), item.getColNum()
					,item.getStock(), item.getRecmdStock());
			String keyString = KeyFactory.keyToString(item.getKey());
			itemInfo.setKeyString(keyString);
			tobacco.add(itemInfo);
		}
		
//		pm.close();
		
		return tobacco;
	}
	
	public List<ItemInfo> getItems(String filter) throws IllegalArgumentException {
		List<ItemInfo> results = new ArrayList<ItemInfo>();

		List<Item> queryResult;
		Iterator itr;
		Item item;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Query qry = pm.newQuery(Item.class);
		qry.setFilter(filter);
		
		queryResult = (List<Item>) qry.execute();
		itr = queryResult.iterator();
		
		// map the results to client side ItemInfo 
		while(itr.hasNext()){
			ItemInfo itemInfo;

			item = (Item) itr.next();
			itemInfo = new ItemInfo(item.getCategory(), item.getBrand(), item.getName(),
					item.getPrice(), item.getSize(), item.getDescription(),
					item.getThumbnail(), item.getImage(), item.getWindowNum(), item.getRowNum(), item.getColNum(),
					item.getStock(), item.getRecmdStock());
			String keyString = KeyFactory.keyToString(item.getKey());
			itemInfo.setKeyString(keyString);
			results.add(itemInfo);
		}
		
		return results;
	}


	@Override
	public Boolean updateItem(String stringKey, ItemInfo itemInfo)
			throws IllegalArgumentException {
		Key key = KeyFactory.stringToKey(stringKey);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			Item item = pm.getObjectById(Item.class, key);
		
			//Compare the difference and update the new value
			if(!itemInfo.getName().equalsIgnoreCase(item.getName())){
				item.setName(itemInfo.getName());
			}
		
			if(!itemInfo.getBrand().equalsIgnoreCase(item.getBrand())){
				item.setBrand(itemInfo.getBrand());
			}
		
			if(itemInfo.getPrice() != item.getPrice()){
				item.setPrice(itemInfo.getPrice());
			}
		
			if(!itemInfo.getSize().equalsIgnoreCase(item.getSize())){
				item.setSize(itemInfo.getSize());
			}
		
			if(!itemInfo.getCategory().equalsIgnoreCase(item.getCategory())){
				item.setCategory(itemInfo.getCategory());
			}
			
			if(!itemInfo.getDescription().equalsIgnoreCase(item.getDescription())){
				item.setDescription(itemInfo.getDescription());
			}
		
			if(!itemInfo.getThumbnail().equalsIgnoreCase(item.getThumbnail())){
				item.setThumbnail(itemInfo.getThumbnail());
			}
		
			if(!itemInfo.getImage().equalsIgnoreCase(item.getImage())){
				item.setImage(itemInfo.getImage());
			}
		
			if(itemInfo.getWindowNum() != item.getWindowNum()){
				item.setWindowNum(itemInfo.getWindowNum());
			}
		
			if(itemInfo.getRowNum() != item.getRowNum()){
				item.setRowNum(itemInfo.getRowNum());
			}
		
			if(itemInfo.getColNum() != item.getColNum()){
				item.setColNum(itemInfo.getColNum());
			}
		
			if(itemInfo.getStock() != item.getStock()){
				item.setStock(itemInfo.getStock());
			}
		
			if(itemInfo.getRecmdStock() != item.getRecmdStock()){
				item.setRecmdStock(item.getRecmdStock());
			}
		}finally{
			pm.close();
		}
		
		return true;
	}
	
}